/*
 * Copyright (C) 2017 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
 * Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "neocomm.h"

#include "globals.hpp"
#include "error.hpp"

#include <opendht.h>
#include <string>
#include <fstream>
#include <stdexcept>

extern "C" {
#include <gnutls/gnutls.h>
}

#define DEFAULT_PORT 1335

dht::DhtRunner node;

int NeoComm_init(const unsigned short port) {
#ifdef WOE32
	gnutls_global_init();
#endif //WOE32
	try {
		node.run((port == 0 ? DEFAULT_PORT : port),
				dht::crypto::generateIdentity(), true);
	} catch(const std::exception &e) {
		add_error(e.what());
		return 0;
	}
	return 1;
}

void NeoComm_deinit() {
	node.join();
#ifdef WOE32
	gnutls_global_deinit();
#endif //WOE32
}

int NeoComm_connect(const char *address, const unsigned short port) {
	if(not node.isRunning())
	{
		add_error("NeoComm must be initialized.");
		return 0;
	}
	node.bootstrap(address, std::to_string(port));
	return 1;
}

int NeoComm_import_nodes(const char *node_file) {
	if(not node.isRunning())
	{
		add_error("NeoComm must be initialized.");
		return 0;
	}
	msgpack::unpacker upak;
	{
		std::ifstream import_file(node_file, std::ios::binary bitor
				std::ios::ate);
		if(not import_file.is_open())
		{
			add_error(std::string("Failed to open file ") + node_file);
			return 0;
		}
		auto file_size = import_file.tellg();
		import_file.seekg(0, std::ios::beg);
		upak.reserve_buffer(file_size);
		import_file.read(upak.buffer(), file_size);
		upak.buffer_consumed(file_size);
	}

	msgpack::object_handle obj_handler;
	while(upak.next(obj_handler))
	{
		auto imported_nodes =
			obj_handler.get().as<std::vector<dht::NodeExport>>();
		node.bootstrap(imported_nodes);
	}
	return 1;
}

int NeoComm_export_nodes(const char *node_file) {
	if(not node.isRunning())
	{
		add_error("NeoComm must be initialized.");
		return 0;
	}
	std::ofstream export_file(node_file, std::ios::binary);
	if(not export_file.is_open())
	{
		add_error(std::string("Failed to open file ") + node_file);
		return 0;
	}
	msgpack::pack(export_file, node.exportNodes());
	return 1;
}

struct NeoComm_stats NeoComm_get_node_stats() {
	struct NeoComm_stats statistics;
	statistics.total = node.getNodesStats(
			AF_UNSPEC,
			&statistics.good,
			&statistics.dubious,
			&statistics.cached,
			&statistics.incoming);

	return statistics;
}
